package cn.com.lyb.flink.combine;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

public class ConnectDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Integer> source1 = env.fromElements(1,2,3,4,5);
        DataStreamSource<String> source2 = env.fromElements("111", "222");
        DataStreamSource<Integer> source3 = env.fromElements(11, 22,33);
        ConnectedStreams<Integer, String> connect = source1.connect(source2);
        SingleOutputStreamOperator<String> map = connect.map(new CoMapFunction<Integer, String, String>() {
            @Override
            public String map1(Integer v1) throws Exception {
                return v1.toString();
            }
            @Override
            public String map2(String v2) throws Exception {
                return v2;
            }
        });
        ConnectedStreams<String, Integer> connect1 = map.connect(source3);
        connect1.map(new CoMapFunction<String, Integer, String>() {
            @Override
            public String map1(String s) throws Exception {
                return s;
            }
            @Override
            public String map2(Integer integer) throws Exception {
                return integer.toString();
            }
        }).print();
        env.execute();
    }
}
